package cn.txy.article.controller;

import cn.txy.article.domain.Article;
import cn.txy.article.query.ArticleQuery;
import cn.txy.article.service.ArticleService;
import cn.txy.config.R;
import cn.txy.system.annotation.PreAuthorize;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api("Article相关的接口")
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询文章",sn="article:getOne",descs = "根据ID查询文章")
    public R findById(@PathVariable("id") Long id){
        return R.ok(articleService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询文章",sn="article:getAll",descs = "查询文章")
    public R findAll(){
        return R.ok(articleService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除文章",sn="article:delOne",descs = "根据ID删除文章")
    public R deleteById(@PathVariable("id")Long id){
        articleService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "批量删除文章",sn="article:deleteAll",descs = "批量删除文章")
    public R batchDelete(@RequestBody List<Long> ids){
        articleService.batchDelete(ids);
        return R.ok();
    }

    @GetMapping("/star/{id}")
    @PreAuthorize(name = "获取点赞数",sn="article:getStartOne",descs = "获取点赞数")
    public R star(HttpServletRequest request,@PathVariable("id")Long id){
        Integer star = articleService.star(request, id);
        return R.ok(star);
    }
    @GetMapping("/read/{id}")
    @PreAuthorize(name = "获取阅读量",sn="article:getReadOne",descs = "获取阅读量")
    public R read(HttpServletRequest request,@PathVariable("id")Long id){
        Integer read = articleService.read(request, id);
        return R.ok(read);
    }

    @GetMapping("/star/top6")
    @PreAuthorize(name = "获取点赞数前六的文章", sn = "article:starTop6", descs = "获取点赞数前六的文章")
    public R getTop6StarArticles() {
        List<Map<String, Object>> top6Articles = articleService.getTop6StarArticles();
        return R.ok(top6Articles);
    }

    @GetMapping("/type/top5")
    @PreAuthorize(name="获取数量最多前六的类型",sn = "article:typeTop5",descs = "获取数量最多前六的类型")
    public R getTop5Type(){
        return R.ok(articleService.getTop5ArticleTypes());
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "添加和修改文章",sn="article:addAndUpdate",descs = "添加和修改文章")
    public R add(@RequestBody Article article){
        if(article.getId() == null){
            article.setCreateTime(new Date());
            articleService.add(article);
        }else {
            article.setUpdateTime(new Date());
            articleService.update(article);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询文章",sn="article:page",descs = "分页查询文章")
    public R getPage(@RequestBody ArticleQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Article> articles = articleService.queryPage(query);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return R.ok(pageInfo);
    }

    @PostMapping("/selectPage")
    @PreAuthorize(name = "分页查询文章",sn="article:selectPage",descs = "分页查询文章")
    public R selectPage(@RequestBody ArticleQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Article> articles = articleService.selectPage(query);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return R.ok(pageInfo);
    }

    @GetMapping("/articleTypeCount")
    @PreAuthorize(name = "文章类型计数",sn="article:articleTypeCount",descs = "文章类型计数")
    public R articleTypeCount(){
        return R.ok(articleService.articleTypeCount());
    }

    @GetMapping("/archives")
    public R archives(){
        return R.ok(articleService.archives());
    }

    @PreAuthorize(name = "获取文章年月",sn = "article:getByYearAndMonth",descs ="获取文章年月")
    @ApiOperation("获取文章年月")
    @GetMapping("/getByYearAndMonth")
    public R getByYearAndMonth(){
        return R.ok(articleService.findByYearAndMonth());
    }

}
